"""
解析土壤水分stm文件
"""
import os

import pandas as pd
import numpy as np


# 单个文件测试
def read_stm(file_path):
    """
    读取stm文件
    :param file_path: stm文件路径
    :return: 返回stm文件中的数据
    """
    head = pd.read_csv(file_path, sep=" ", header=None, nrows=1)
    print(head)
    print("数据类型：", head.iloc[0, 0])
    print("站点：", head.iloc[0, 4])
    print("纬度：", head.iloc[0, 17])
    print("经度：", head.iloc[0, 18])
    print("高程：", head.iloc[0, 35])
    print("起始深度：", head.iloc[0, 36])
    print("截止深度：", head.iloc[0, 37])
    data = pd.read_csv(file_path, sep=" ", header=None, skiprows=1, names=["date", "time", "value"], usecols=[0, 1, 2])
    print(data.shape)
    date_list = []
    mean_list = []
    mean_value_list = []
    for i in range(data.shape[0]):
        date = data.iloc[i, 0]
        if date not in date_list:
            if len(mean_value_list) == 24:
                mean_list.append(np.mean(mean_value_list))
            date_list.append(date)
            mean_value_list = [data.iloc[i, 2]]
            # 如果已经是最后一个数据了，就直接存了
            if i == (data.shape[0] - 1):
                mean_list.append(np.mean(mean_value_list))
        else:
            mean_value_list.append(data.iloc[i, 2])

    # for i in range(len(date_list)):
    #     print(date_list[i], mean_list[i])


def main(CTP_SMTMN_path, save_path):
    ctp_smtmn = pd.DataFrame(columns=('station', 'date', 'start_deep', 'end_deep', 'elev', 'lon', 'lat', 'mean_value'))
    # 将所有数据存成一个列表，需要的站点数据为L06-09，L11-13，M07-08，M11-12，M15，M18-20，S02-07，一共21个站点
    station_dir_list = os.listdir(CTP_SMTMN_path)
    # print(station_dir_list)
    for station_name in station_dir_list:
        # if station_name in ["L06-M10", "L07-M13", "L08-M14", "L09-M16", "L11-M21",
        #                     "L12-M22", "L13", "M07-S01", "M08", "M11-S09", "M12-S08",
        #                     "M15", "M18", "M19", "M20", "S02", "S03", "S04", "S05", "S06", "S07"]:
        print("站点{}读取中...".format(station_name))
        station_path = os.path.join(CTP_SMTMN_path, station_name)
        # print(station_path)
        # 获取文件夹下后缀名为stm的文件
        file_list = [os.path.join(station_path, file) for file in os.listdir(station_path) if file.endswith(".stm")]
        # print(file_list)
        for file in file_list:
            head = pd.read_csv(file, sep=" ", header=None, nrows=1)
            station = head.iloc[0, 4]  # 站点
            if station in ["L04_M02", "L05_M06", "L06_M10", "L07_M13", "L08_M14", "L09_M16", "L10_M17", "L11_M21", "L12_M22", "M07_S01", "M11_S09", "M12_S08"]:
                lat = head.iloc[0, 13]  # 纬度
                lon = head.iloc[0, 14]  # 经度
                elev = head.iloc[0, 31]  # 海拔
                start_deep = head.iloc[0, 32]  # 开始深度
                end_deep = head.iloc[0, 33]  # 截止深度
            else:
                lat = head.iloc[0, 17]  # 纬度
                lon = head.iloc[0, 18]  # 经度
                elev = head.iloc[0, 35]  # 海拔
                start_deep = head.iloc[0, 36]  # 开始深度
                end_deep = head.iloc[0, 37]  # 截止深度

            data = pd.read_csv(file, sep=" ", header=None, skiprows=1, names=["date", "time", "value"], usecols=[0, 1, 2])
            # print(data.shape)
            date_list = []
            mean_list = []
            mean_value_list = []
            for i in range(data.shape[0]):
                date = data.iloc[i, 0].replace("/", "")
                if date not in date_list:
                    if len(mean_value_list) > 0:
                        mean_list.append(np.mean(mean_value_list))
                    date_list.append(date)
                    mean_value_list = [data.iloc[i, 2]]
                    # 如果已经是最后一个数据了，就直接存了
                    if i == (data.shape[0] - 1):
                        mean_list.append(np.mean(mean_value_list))
                else:
                    mean_value_list.append(data.iloc[i, 2])

            for i in range(len(date_list)):
                # print(len(date_list))
                # print(len(mean_list))
                date = date_list[i]
                mean_value = mean_list[i]
                # 存入pandas
                ctp_smtmn = ctp_smtmn.append(pd.DataFrame({'station': [station], 'date': [date], 'start_deep': [start_deep], 'end_deep': [end_deep],
                                                           'elev': [elev], 'lon': [lon], 'lat': [lat], 'mean_value': [mean_value]}), ignore_index=True)
    ctp_smtmn.to_csv(save_path, sep=",", index=False, encoding='utf-8-sig')
    print("数据写入完成")


if __name__ == '__main__':
    # file_path = r"G:\遥感图像处理\土壤水分降尺度\Data_separate_files_header_20130726_20130824_10440_jqzQ_20230727\CTP_SMTMN\L13\CTP-SMTMN_CTP-SMTMN_L13_sm_0.000000_0.050000_EC-TM_20130726_20130824.stm"
    # read_stm(file_path)
    CTP_SMTMN_path = r"G:\遥感图像处理\土壤水分降尺度\Data_separate_files_header_20130726_20130824_10440_jqzQ_20230727\CTP_SMTMN"
    save_path = r"G:\test\process_result\ctp_smtmn_all.csv"
    main(CTP_SMTMN_path, save_path)
